using UnityEngine;
using System.Collections.Generic;
using UndergroundSurvival.Managers;

namespace UndergroundSurvival.Rooms
{
    public class BaseRoom : Room
    {
        [Header("Room Functionality")]
        [SerializeField] protected float productionRate = 1f;
        [SerializeField] protected float consumptionRate = 1f;
        [SerializeField] protected List<ResourceCost> productionOutputs;
        [SerializeField] protected List<ResourceCost> consumptionInputs;

        protected float productionTimer = 0f;
        protected float consumptionTimer = 0f;
        protected bool isFunctioning = false;

        protected virtual void Update()
        {
            if (!isBuilt || !isFunctioning) return;

            // 处理资源消耗
            if (consumptionInputs != null && consumptionInputs.Count > 0)
            {
                consumptionTimer += Time.deltaTime;
                if (consumptionTimer >= 1f / consumptionRate)
                {
                    ConsumeResources();
                    consumptionTimer = 0f;
                }
            }

            // 处理资源生产
            if (productionOutputs != null && productionOutputs.Count > 0)
            {
                productionTimer += Time.deltaTime;
                if (productionTimer >= 1f / productionRate)
                {
                    ProduceResources();
                    productionTimer = 0f;
                }
            }
        }

        protected virtual void ConsumeResources()
        {
            if (ResourceManager.Instance == null) return;

            foreach (var input in consumptionInputs)
            {
                if (!ResourceManager.Instance.HasEnoughResource(input.resourceName, input.amount))
                {
                    isFunctioning = false;
                    return;
                }
            }

            ResourceManager.Instance.ConsumeResources(consumptionInputs);
            isFunctioning = true;
        }

        protected virtual void ProduceResources()
        {
            if (ResourceManager.Instance == null || !isFunctioning) return;

            foreach (var output in productionOutputs)
            {
                ResourceManager.Instance.AddResourceAmount(output.resourceName, output.amount);
            }
        }

        public override void OnRoomBuilt()
        {
            base.OnRoomBuilt();
            isFunctioning = true;
        }

        public override void OnRoomDamaged(float damage)
        {
            base.OnRoomDamaged(damage);
            if (health < maxHealth * 0.3f)
            {
                isFunctioning = false;
            }
        }

        public override void OnRoomRepaired(float amount)
        {
            base.OnRoomRepaired(amount);
            if (health >= maxHealth * 0.3f)
            {
                isFunctioning = true;
            }
        }

        public virtual void SetProductionRate(float rate)
        {
            productionRate = rate;
        }

        public virtual void SetConsumptionRate(float rate)
        {
            consumptionRate = rate;
        }
    }
} 